package com.shang.controller;

import com.shang.enums.OrderStatusEnum;
import com.shang.pojo.OrderStatus;
import com.shang.pojo.UserAddress;
import com.shang.pojo.bo.AddressBO;
import com.shang.pojo.bo.ShopCartBO;
import com.shang.pojo.bo.SubmitOrderBO;
import com.shang.pojo.vo.MerchantOrdersVO;
import com.shang.pojo.vo.OrderVO;
import com.shang.service.AddressService;
import com.shang.service.OrderService;
import com.shang.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "订单相关")
@RequestMapping("orders")
@RestController
public class OrdersController extends BaseController{

    @Autowired
    private AddressService addressService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "地址查询")
    @PostMapping("/create")
    public ShangJSONResult create(@RequestBody SubmitOrderBO submitOrderBO, HttpServletRequest request, HttpServletResponse response){


        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + submitOrderBO.getUserId());
        if (StringUtils.isBlank(shopcartJson)){
            return ShangJSONResult.errorMsg("购物车数据不正确");
        }

        List<ShopCartBO> shopCartBOList = JsonUtils.jsonToList(shopcartJson, ShopCartBO.class);

        //创建订单
        OrderVO orderVO = orderService.createOrder(shopCartBOList,submitOrderBO);
        String orderId = orderVO.getOrderId();
        MerchantOrdersVO merchantOrdersVO = orderVO.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(payReturnUrl);

        //测试时统一设置为1分钱
        merchantOrdersVO.setAmount(1);

        //覆盖现有redis中的购物车数据
        shopCartBOList.removeAll(orderVO.getToBeRemovedShopcartList());
        redisOperator.set(FOODIE_SHOPCART + ":" + submitOrderBO.getUserId(),JsonUtils.objectToJson(shopCartBOList));
        //整合redis 清空前端购物车cookie
        CookieUtils.setCookie(request,response,FOODIE_SHOPCART,JsonUtils.objectToJson(shopCartBOList),true);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add("imoocUserId","imooc");
        httpHeaders.add("password","imooc");

        HttpEntity<MerchantOrdersVO> entity = new HttpEntity<>(merchantOrdersVO, httpHeaders);
        ResponseEntity<ShangJSONResult> responseEntity = restTemplate.postForEntity(paymentUrl, entity, ShangJSONResult.class);

        ShangJSONResult paymentResult = responseEntity.getBody();
        if (paymentResult.getStatus() != 200){
            return ShangJSONResult.errorMsg("支付订单创建失败");
        }

        return ShangJSONResult.ok(orderId);
    }


    @PostMapping("/notifyMerchantOrderPaid")
    public Integer notifyMerchantOrderPaid(String merchantOrderId){
        orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return HttpStatus.OK.value();
    }

    @PostMapping("/getPaidOrderInfo")
    public ShangJSONResult getPaidOrderInfo(String orderId){

        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);

        return ShangJSONResult.ok(orderStatus);
    }

}
